
package dto;

import com.j256.ormlite.field.DatabaseField;
import exceptions.EventException;
import gui.data.ITrackEventData;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Event head class, all Events inherit this class
 * 
 * @author steffen
 */
public class Event implements ITrackEventData, Comparable<Event> {
    
    public static final String TRACK_TABLE_NAME = "track";
    public static final String TIME_TABLE_NAME = "time";
    public static final String DESC_TABLE_NAME = "desc";
    public static final String LATITUDE_TABLE_NAME = "lat";
    public static final String LONGITUDE_TABLE_NAME = "lon";

    @DatabaseField(columnName = DESC_TABLE_NAME)
    protected String description;

    @DatabaseField(foreign = true, columnName = TRACK_TABLE_NAME)
    protected Track track;    // The Track which contains this Event
    
    @DatabaseField(columnName = TIME_TABLE_NAME)
    protected long timestamp;
    
    @DatabaseField(columnName = LATITUDE_TABLE_NAME)
    protected float latitude;
    
    @DatabaseField(columnName = LONGITUDE_TABLE_NAME)
    protected float longitude;
    
    @DatabaseField(generatedId = true)
    protected int id;
    
    private GeoPoint pos;
  
    public Event (Track track, String description, long timestamp, GeoPoint pos) throws EventException {
        if (track == null) throw new EventException(EventException.TRACK);
        if (description == null) throw new EventException(EventException.DESCRIPTION);
        
        this.track = track;
        this.description = description;
        this.timestamp = timestamp;
        setPos(pos);
    }
    
    public Event() {} // Needed for OrmLite
    
    public Track getTrack() {
        return track;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) throws EventException {
        if (description == null)
            throw new EventException(EventException.DESCRIPTION);
        this.description = description;
    }
    
    /**
     * Returns the type of the event
     */
    @Override
    public ITrackEventData.EventType getEventType() {
        if (this instanceof TextEvent) {
            return EventType.Text;
        }
        else if (this instanceof ImageEvent) {
            return EventType.Image;
        }
        else {
            return EventType.Video;
        }
    }
    
    /**
     * Returns the name of the event
     */
    @Override
    public String getEventName() {
        return description;
    }
    
    /**
     * Gets the Date of the event
     */
    @Override
    public Calendar getEventDate() {
        Calendar c = new GregorianCalendar();
        c.setTimeInMillis(timestamp);
        return c;
    }
    
    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
    
    public void setParentTrack(Track track) {
        this.track = track;
    }
    
    public GeoPoint getPos() {
        if (pos == null) {
            pos = new GeoPoint(latitude, longitude);
        }
        
        return pos;
    }

    public void setPos(GeoPoint pos) {
        this.pos = pos;
        this.latitude = pos.getLanDeg();
        this.longitude = pos.getLonDeg();
    }
    
    public void setLatitude(float latitude) {
        this.latitude = latitude;
        this.pos = new GeoPoint(latitude, longitude);
    }
    
    public void setLongitude(float longitude) {
        this.longitude = longitude;
        this.pos = new GeoPoint(latitude, longitude);
    }

    @Override
    public int compareTo(Event o) {
        if (this.timestamp == o.timestamp) {
            return 0;
        }
        else if (this.timestamp > o.timestamp) {
            return 1;
        }
        else {
            return -1;
        }
    }
}
